<template>
  <div class="login">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span class="el-card__header">后台管理系统</span>
        <el-form :model="form" status-icon ref="form" label-width="80px">
          <el-form-item
            label="用户名"
            prop="username"
            :rules="[
              { required: true, message: '输入用户名', trigger: 'blur' },
              {
                min: 4,
                max: 10,
                message: '长度4-10字符',
                trigger: 'blur',
              },
            ]"
          >
            <el-input type="text" v-model="form.username"></el-input>
          </el-form-item>
          <el-form-item
            label="密码"
            prop="password"
            :rules="[
              { required: true, message: '输入密码', trigger: 'blur' },
              {
                min: 6,
                max: 12,
                message: '长度6-12字符',
                trigger: 'blur',
              },
            ]"
          >
            <el-input type="password" v-model="form.password"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="login('form')">提交</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import {loginApi} from '@/http'
export default {
  name: "",
  props: [""],
  data() {
    return {
      form: {
        username: "",
        password: "",
      },
    };
  },

  components: {},

  computed: {},

  created() {},

  mounted() {},

  methods: {
    login(form) {
      // console.log(this.form);
      //获取信息
      this.$refs[form].validate((valid) => {
        // console.log(this.form);
        if (valid) {
          this.servers
            .post(
              "/login",
              this.form
            )
            .then((res) => {
              console.log(res);
              if (res.status == 200) {
                localStorage.setItem("username", res.data.data.username);

                this.$message({
                  message: "恭喜你登录成功",
                  type: "success",
                });
                //跳转网页
                this.$router.push("/home");
              }
            })
            .catch((err) => {
              console.log(err);
            });
          // loginApi(this.form).then(res=>{
          //   console.log(res);
          //                 if (res.status == 200) {
          //       localStorage.setItem("username", JSON.parse(res.config.data).parms.username);

          //       this.$message({
          //         message: "恭喜你登录成功",
          //         type: "success",
          //       });
          //       //跳转网页
          //       this.$router.push("/home");
          //     }
          // })
        } else {
          console.error(this.form);
        }
      });
    },
  },
};
</script>

<style lang='less' scoped>
.login {
  width: 100%;
  height: 100%;
  position: absolute;
  // background: #409eff;
  background: url(../assets/bg.png);
  .box-card {
    width: 450px;
    margin: 200px auto;
    .el-card__header {
      font-size: 30px;
      margin-left: 100px;
      
    }
    .el-button {
      width: 100%;
    }
  }
}
</style>